package jxtend.processor.eclipse.visitor.replace;


import org.eclipse.jdt.internal.compiler.ast.ASTNode;
import org.eclipse.jdt.internal.compiler.ast.LocalDeclaration;
import org.eclipse.jdt.internal.compiler.ast.TypeReference;

@SuppressWarnings("restriction")
public class LocalDeclarationTypeReplacer implements ASTNodeReplaceHandler {

	@Override
	public boolean replaceNode(ASTNode parentNode, ASTNode originalNode, ASTNode newNode) {
		if (!(parentNode instanceof LocalDeclaration)
				|| (!(originalNode instanceof TypeReference)
				|| !(newNode instanceof TypeReference))) {
			return false;
		}

		LocalDeclaration localDeclaration = (LocalDeclaration) parentNode;

		if (localDeclaration.type != originalNode) {
			throw new IllegalStateException(
					"Could not replace type, it's not the same as the type of the LocalDeclaration");
		}

		localDeclaration.type = (TypeReference) newNode;
		
		return true;
	}

}
